package com.soriya.nestlive.util.handler;

import com.soriya.nestlive.util.ExceptionUtil;
import com.soriya.nestlive.util.excpetion.MyException;
import com.soriya.nestlive.util.result.Result;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 自定义异常处理器
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public Result<?> myExceptionHandler(MyException e) {
        log.error(ExceptionUtil.getMessage(e));
        return Result.failed().setCode(e.getCode()).setMessage(e.getMsg());
    }

    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseBody
    public Result<?> tokenExpired() {
        return Result.failed().setMessage("登录信息已失效, 请重新登录").setCode(514);
    }

    // 全局异常处理器
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<?> error(Exception e) {
        log.error(ExceptionUtil.getMessage(e));
        return Result.failed().setMessage(e.getMessage());
    }

}
